package com.example.sdktest;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.example.sdktest.R.id;
import com.homer.iot.esptouch.model.Device;
import com.homer.iot.esptouch.model.Device.eRegularAction;
import com.homer.iot.esptouch.model.Device.eRegularType;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.Time;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.TimePicker;

public class RegularConfig extends Activity {
	
	TimePicker timePicker;
	Button btnSetAlarmRegularOn;
	Button btnSetAlarmRegularOff;
	Button btnCancelAlarmRegular;
	
	Button btnSetLightRegularOn;
	Button btnSetLightRegularOff;
	Button btnCancelLightRegular;
	
	Button btnSetSoundRegularOn;
	Button btnCancelSoundRegular;
	
	RadioButton radioDay;
	RadioButton radioHour;
	RadioButton radioMinute;
	RadioButton radioOneMinute;
	
	String devIp;
    String devId;
    byte alarmOnId = (byte)0x00;
    byte alarmOffId = (byte)0x01;
    byte lightOnId = (byte)0x02;
    byte lightOffId = (byte)0x03;
    byte soundId = (byte)0x04;
    
	Device devForTest;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_regular_config);
		
		Intent intent = getIntent();
		devId = intent.getExtras().getString("devId");
		devIp = intent.getExtras().getString("devIp");
		
		timePicker = (TimePicker)findViewById(id.timePicker1);
		btnSetAlarmRegularOn = (Button)findViewById(id.btnSetAlarmRegularOn);
		btnSetAlarmRegularOff = (Button)findViewById(id.btnSetAlarmRegularOff);
		btnCancelAlarmRegular = (Button)findViewById(id.btnCancelRegularAlarm);
		btnSetLightRegularOn = (Button)findViewById(id.btnSetLightRegularOn);
		btnSetLightRegularOff = (Button)findViewById(id.btnSetLightRegularOff);
		btnCancelLightRegular = (Button)findViewById(id.btnCancelRegularLight);
		btnSetSoundRegularOn = (Button)findViewById(id.btnSetSoundRegularOn);
		btnCancelSoundRegular = (Button)findViewById(id.btnCancelRegularSound);
		radioDay = (RadioButton)findViewById(id.radioDay);
		radioHour = (RadioButton)findViewById(id.radioHour);
		radioMinute = (RadioButton)findViewById(id.radioMinute);
		radioOneMinute = (RadioButton)findViewById(id.radioOneMinute);
		
		btnSetAlarmRegularOn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						int hour = timePicker.getCurrentHour();
						int minute = timePicker.getCurrentMinute();
						devForTest = initDevSingle();
						
						eRegularAction regularAction = eRegularAction.Once;
						if(radioDay.isChecked()) {regularAction = eRegularAction.EachDay;}
						if(radioHour.isChecked()) {regularAction = eRegularAction.EachHour;}
						if(radioMinute.isChecked()) {regularAction = eRegularAction.EachFiveMinute;}
						if(radioOneMinute.isChecked()) {regularAction = eRegularAction.EachMinute;}
						
						Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
						cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), hour, minute, cal.get(Calendar.SECOND));;
						devForTest.setRegular(alarmOnId, eRegularType.AlarmOn, regularAction, cal);
					}
				}).start();
			}
		});
		
		btnSetAlarmRegularOff.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						int hour = timePicker.getCurrentHour();
						int minute = timePicker.getCurrentMinute();
						devForTest = initDevSingle();
						
						eRegularAction regularAction = eRegularAction.Once;
						if(radioDay.isChecked()) {regularAction = eRegularAction.EachDay;}
						if(radioHour.isChecked()) {regularAction = eRegularAction.EachHour;}
						if(radioMinute.isChecked()) {regularAction = eRegularAction.EachFiveMinute;}
						if(radioOneMinute.isChecked()) {regularAction = eRegularAction.EachMinute;}
						
						Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
						cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), hour, minute, cal.get(Calendar.SECOND));;
						devForTest.setRegular(alarmOffId, eRegularType.AlarmOff, regularAction, cal);
					}
				}).start();
			}
		});
		
		btnCancelAlarmRegular.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
					devForTest = initDevSingle();
					devForTest.disableRegular(alarmOnId);
					devForTest.disableRegular(alarmOffId);
					}
				}).start();
			}
		});
		
		btnSetLightRegularOn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						int hour = timePicker.getCurrentHour();
						int minute = timePicker.getCurrentMinute();
						devForTest = initDevSingle();
						
						eRegularAction regularAction = eRegularAction.Once;
						if(radioDay.isChecked()) {regularAction = eRegularAction.EachDay;}
						if(radioHour.isChecked()) {regularAction = eRegularAction.EachHour;}
						if(radioMinute.isChecked()) {regularAction = eRegularAction.EachFiveMinute;}
						if(radioOneMinute.isChecked()) {regularAction = eRegularAction.EachMinute;}
						
						Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
						cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), hour, minute, cal.get(Calendar.SECOND));;
						devForTest.setRegular(lightOnId, eRegularType.LightOn, regularAction, cal);
					}
				}).start();
			}
		});
		
		btnSetLightRegularOff.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						int hour = timePicker.getCurrentHour();
						int minute = timePicker.getCurrentMinute();
						devForTest = initDevSingle();
						
						eRegularAction regularAction = eRegularAction.Once;
						if(radioDay.isChecked()) {regularAction = eRegularAction.EachDay;}
						if(radioHour.isChecked()) {regularAction = eRegularAction.EachHour;}
						if(radioMinute.isChecked()) {regularAction = eRegularAction.EachFiveMinute;}
						if(radioOneMinute.isChecked()) {regularAction = eRegularAction.EachMinute;}
						
						Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
						cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), hour, minute, cal.get(Calendar.SECOND));;
						devForTest.setRegular(lightOffId, eRegularType.LightOff, regularAction, cal);
					}
				}).start();
			}
		});
		
		btnCancelLightRegular.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new Thread(new Runnable() {
					public void run() {
						// TODO Auto-generated method stub
					devForTest = initDevSingle();
					devForTest.disableRegular(lightOnId);
					devForTest.disableRegular(lightOffId);
					}
				}).start();
			}
		});
		
		btnSetSoundRegularOn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						int hour = timePicker.getCurrentHour();
						int minute = timePicker.getCurrentMinute();
						devForTest = initDevSingle();
						
						eRegularAction regularAction = eRegularAction.Once;
						if(radioDay.isChecked()) {regularAction = eRegularAction.EachDay;}
						if(radioHour.isChecked()) {regularAction = eRegularAction.EachHour;}
						if(radioMinute.isChecked()) {regularAction = eRegularAction.EachFiveMinute;}
						if(radioOneMinute.isChecked()) {regularAction = eRegularAction.EachMinute;}
						
						Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
						cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), hour, minute, cal.get(Calendar.SECOND));;
						devForTest.setRegular(soundId, eRegularType.Clock, regularAction, cal);
					}
				}).start();
			}
		});
		
		btnCancelSoundRegular.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						devForTest = initDevSingle();
						devForTest.disableRegular(soundId);
					}
				}).start();
			}
		});
	}
	
	private Device initDevSingle()
	{
		if(devForTest == null)
		{
			devForTest = new Device(devIp, devId);
		}
		return devForTest;
	}
}
